<?php
class NewTaskService {

    /**
     * @var NewTaskService
     */
    private static $instance;

    /**
     * Get the instance of NewTaskService
     * @return NewTaskService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new NewTaskService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    public function __construct() {

    }

    /**
     * 更新任务进度
     * @param $userId
     * @param $progress
     * @param $content
     * @throws SzException
     */
    public function updateProgress($userId,$content,$progress) {
        if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_GRANDMA,true)) {
            return;
        }
        /**
         * @var $userTaskBehaviorList NewTaskBehaviorVoList
         * @var $userTaskBehavior NewTaskBehaviorVo
         */
        $userTaskBehaviorList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK_BEHAVIOR);
        $userTaskBehavior = $userTaskBehaviorList->getElement($content,false);
        if ($userTaskBehavior) {
            if ($userTaskBehavior->getProgress() > 99999999) {
                return;
            }
            $userTaskBehavior->updateProgress($progress);
            if ($_SERVER['is_payment_callback'] ?? false) $userTaskBehavior->persist();
            $userTaskBehaviorList->updateElement($content,$userTaskBehavior);
        } else {
            $userTaskBehavior = new NewTaskBehaviorVo($userId,$content,$progress,true);
            if ($_SERVER['is_payment_callback'] ?? false) $userTaskBehavior->persist();
            $userTaskBehaviorList->addElement($userTaskBehavior);
            $userTaskBehaviorList->setElement($content,$userTaskBehavior);
        }
        if ($_SERVER['is_payment_callback'] ?? false) $userTaskBehaviorList->persist();
        SzPersister::get()->setVoList($userTaskBehaviorList);
        return;
    }

    /**
     * 更新任务进度
     * @param $userId
     * @param $progress
     * @param $content
     * @throws SzException
     */
    public function setProgress($userId,$content,$progress) {
        if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_GRANDMA,true)) {
            return;
        }
        /**
         * @var $userTaskBehaviorList NewTaskBehaviorVoList
         * @var $userTaskBehavior NewTaskBehaviorVo
         */
        $userTaskBehaviorList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK_BEHAVIOR);
        $userTaskBehavior = $userTaskBehaviorList->getElement($content,false);
        if ($userTaskBehavior) {
            if ($userTaskBehavior->getProgress() > 99999999) {
                return;
            }
            $userTaskBehavior->setProgress($progress);
            $userTaskBehaviorList->updateElement($content,$userTaskBehavior);
        } else {
            $userTaskBehavior = new NewTaskBehaviorVo($userId,$content,$progress,true);
            $userTaskBehaviorList->addElement($userTaskBehavior);
            $userTaskBehaviorList->setElement($content,$userTaskBehavior);
        }
        SzPersister::get()->setVoList($userTaskBehaviorList);
        return;
    }

    /**
     * 初始化数据
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function taskInfo($userId) {
        /**
         * @var $userTaskList NewTaskVoList
         * @var $userTask NewTaskVo
         * @var $userTaskBehaviorList NewTaskBehaviorVoList
         */
        $userTaskList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK);
        $grandmaSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GRANDMA , null, true);
        if ($grandmaSettings) {
            foreach ($grandmaSettings as $grandmaSetting) {
                if (!$userTaskList->getElement($grandmaSetting['id'],false)) {
                    $userTask = new NewTaskVo($userId,$grandmaSetting['id'],0,true);
                    $userTaskList->addElement($userTask);
                    $userTaskList->setElement($grandmaSetting['id'],$userTask);
                }
            }
            SzPersister::get()->setVoList($userTaskList);
        }

        $userTaskBehaviorList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK_BEHAVIOR);

//        /**
//         * @var $friendVo NewFriendsVo
//         * @var $userFriendVo NewFriendsVo
//         */
//        $userFriendVoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_NEW_FRIEND);
//        $inviteSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_INVITE, null, true);
//        $inviteCount = 0;
//        foreach ($userFriendVoList->getList() as $friendVo) {
//            if ($friendVo->getInviteId() != -1 && ($inviteSettings[$friendVo->getInviteId()]  || $friendVo->getInviteId() == GameConst::INVITE_FRIEND_SHARE_ID)) {
//                $inviteCount++;
//            }
//        }
//        $inviteCount = NewFriendService::get()->getInviteNum($userId,$inviteCount);
//        NewTaskService::get()->setProgress($userId,GameConst::TASK_INVITE_FRIEND,$inviteCount);

        return array(
            'taskInfo' => $userTaskList->toArray(),
            'progress' => $userTaskBehaviorList->toArray()
        );
    }

    /**
     * @param $userId
     * @param $taskId
     * @throws SzException
     */
    public function reward($userId,$taskId) {
        /**
         * @var $userTaskList NewTaskVoList
         * @var $userTask NewTaskVo
         * @var $userTaskBehaviorList NewTaskBehaviorVoList
         * @var $userTaskBehavior NewTaskBehaviorVo
         */
        $grandmaSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GRANDMA , $taskId, true);
        if (!$grandmaSetting || !$grandmaSetting['conditionId']) {
            throw new SzException(80001,GameConst::CONFIG_GRANDMA.'-'.$taskId);
        }

        $userTaskList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK);
        $userTask = $userTaskList->getElement($taskId);
        if ($userTask->getStatus()) {
            throw new SzException(90007);
        }

        $conditionIds = $grandmaSetting['conditionId'];
        $taskGrandmaSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_TASK_GRANDMA , null, true);

        $userTaskBehaviorList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TASK_BEHAVIOR);
        foreach ($conditionIds as $conditionId) {
            if (!isset($taskGrandmaSettings[$conditionId])) {
                throw new SzException(80001,GameConst::CONFIG_TASK_GRANDMA.'-'.$conditionId);
            }
            $taskGrandmaSetting = $taskGrandmaSettings[$conditionId];
            $userTaskBehavior = $userTaskBehaviorList->getElement($taskGrandmaSetting['content'],false);
            if (!$userTaskBehavior || $userTaskBehavior->getProgress() < $taskGrandmaSetting['count']) {
                throw new SzException(90051);
            }
        }

        $outputTid = '';
        foreach ($grandmaSetting['reward'] as $entityId=>$count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $outputTid .= $entityId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GRANDMA,$userId,1,$outputTid,'reward');//获得资源上报

        $userTask->setStatus(1);
        $userTaskList->updateElement($taskId,$userTask);
        SzPersister::get()->setVoList($userTaskList);

        // 数据采集:行为
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:grandma|action:receive|',
                'item' => $taskId,
                'value' => 1
            )
        ));

        // 数据采集:获得
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $outputTid,
                'item' => 'grandma_receive&'.$taskId,
            )
        ));

        return true;
    }

    /**
     * 兑换
     * @param $userId
     * @param $exchangeId
     * @return bool
     * @throws SzException
     */
    public function exchange($userId,$exchangeId) {
        $exchangeGrandmaSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_EXCHANGE_GRANDMA  , $exchangeId, true);
        if (!$exchangeGrandmaSetting) {
            throw new SzException(80001,GameConst::CONFIG_EXCHANGE_GRANDMA.'-'.$exchangeId);
        }
        $outputTid = $inputTid = '';
        $amount = 0;
        foreach ( $exchangeGrandmaSetting['expend'] as $entityId=>$count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $inputTid .= $entityId.':'.$count.'|';
            if ($entityId == GameConst::ENTITY_ACTION_CONSUME_MONEY) {
                $amount += $count;
            }

        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GRANDMA,$userId,0,$inputTid,'exchange');//消耗资源上报

        foreach ( $exchangeGrandmaSetting['reward'] as $entityId=>$count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $outputTid .= $entityId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GRANDMA,$userId,1,$outputTid,'exchange');//获得资源上报


        // 数据采集:行为
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:grandma|action:buy|',
                'item' => $exchangeId,
                'value' => 1
            )
        ));

        // 数据采集:获得
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $outputTid,
                'item' => 'grandmabuy&'.$exchangeId,
            )
        ));

        // 数据采集:消耗
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $inputTid,
                'item' => 'grandmabuy&'.$exchangeId,
            )
        ));

        if ($amount) {
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USE_GOLD, array(
                'clientData' => array(
                    'item' => 'grandmabuy&'.$exchangeId,
                    'value' => $amount
                )
            ));
        }

        return true;
    }

    /**
     * 初始化老奶奶的建筑物
     * @param $userId
     * @throws SzException
     */
    public function initTaskBuilding($userId) {
        $resourceSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GROUND_RESOURCE, 10010143, true);
        if (!$resourceSetting) {
            return;
        }
        $buildingSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_BUILDING, 28911001, true);
        if (!$buildingSetting) {
            return;
        }

        $groundList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GROUND);
        $gridId = GroundService::get()->parseCoordinate($resourceSetting['anchorX'], $resourceSetting['anchorY']);
        /**
         * @var GroundVo $ground
         * @var BuildingVo $building
         * @var GroundVo $ground
         */
        $ground = $groundList->getElement($gridId, false);
        if ($ground) {
            return;
        }
        $buildingList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_BUILDING);
        $buildingDefId = $resourceSetting['itemDefId'];
        $orient = $resourceSetting['direction'];
        /** @var SzMySqlModel $buildingModel */
        $building = BuildingService::get()->create($userId, $buildingDefId, $orient, GameConst::DEFAULT_BOOL_OF_TRUE, GameConst::BUILDING_IS_BUILD, GameService::get()->getReqTime());
        $buildingModel = SzPersister::get()->getModel(GameConst::ORM_NAME_BUILDING);
        list($affectedRows, $decorId) = $buildingModel->insert($building);
        if ($affectedRows) {
            $building->setDecorId($decorId);
            $building->removeInsertFlag();
            $buildingList->setElement($decorId, $building);
            $buildingModel->setListOfVoCache(array($building), $buildingList->getList());
            SzPersister::get()->addManuallyInsertedResponse($building, $userId, $decorId);
        }
        $ground = $groundList->getElement($gridId, false);
        // 更新地块信息
        GroundService::get()->updateGroundWithGridId($userId, $gridId, $buildingSetting['layer'], $decorId, $ground, $groundList, GameConst::GROUND_LAYER_TYPE_DECORATION);
        SzPersister::get()->setVoList($groundList);
        SzPersister::get()->setVoList($buildingList);
        return;
    }

    /**
     * 初始化任务
     * @param $userId
     * @param $isUpLevel
     * @throws SzException
     */
    public function initDailyTask($userId, $isUpLevel=false) {
        if ($isUpLevel) {
            $sysConfig = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_SYSTEM_UNLOCK, GameConst::FUNC_DAILY_TASK, true);
            if ($sysConfig['level'] > (UserService::get()->getUserLevel($userId) + 1)) {
                return;
            }
        } else {
            if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_DAILY_TASK,true)) {
                return;
            }
        }
        /**
         * @var $newDailyTaskInfoList NewDailyTaskInfoVoList
         * @var $newDailyTaskInfo NewDailyTaskInfoVo
         * @var $newDailyTaskList NewDailyTaskVoList
         * @var $newDailyTask NewDailyTaskVo
         */
        $newDailyTaskInfoList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK_INFO);
        $newDailyTaskInfo = $newDailyTaskInfoList->getElement(GameConst::DAILY_TASK_INFO_PRIMARY_KEY,false);
        if (!$newDailyTaskInfo) {
            $newDailyTaskInfo = new NewDailyTaskInfoVo($userId,GameConst::DAILY_TASK_INFO_PRIMARY_KEY,0,0,true);
        }
        $serverTime = GameService::get()->getReqTime();
        if (date('Y-m-d',$serverTime) == date('Y-m-d',$newDailyTaskInfo->getUpdateTime())) {
            return;
        }
        $dailyTaskSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TASK_DAILY, null, true);
        if (!$dailyTaskSettings) {
            return;
        }

        $fixedList = $unFixedList = array();
        foreach ($dailyTaskSettings as $dailyTaskSetting) {
            if ($dailyTaskSetting['type']) {
                $fixedList[$dailyTaskSetting['taskId']] = $dailyTaskSetting;
            } else {
                $unFixedList[$dailyTaskSetting['taskId']] = $dailyTaskSetting;
            }
        }

        if (count($unFixedList) > 2) {
            $keys = array_rand($unFixedList,2);
            $data = array(
                $keys[0] => $unFixedList[$keys[0]],
                $keys[1] => $unFixedList[$keys[1]],
            );
            $unFixedList = $data;
        }
        
        $newDailyTaskList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK);

        if ($newDailyTaskList->toArray()) {
            foreach ($newDailyTaskList->toArray() as $val) {
                $newDailyTask = $newDailyTaskList->getElement($val['taskId']);
                $newDailyTask->setStatus(0);
                $newDailyTask->setNumber(0);
                $newDailyTask->setMaxNum(0);
                $newDailyTask->setReceiveStatus(0);
                $newDailyTaskList->updateElement($val['taskId'],$newDailyTask);
            }
        }
        if ($unFixedList) {
            foreach ($unFixedList as $taskInfo) {
                $newDailyTask = $newDailyTaskList->getElement($taskInfo['taskId'],false);
                if (!$newDailyTask) {
                    $newDailyTask = new NewDailyTaskVo($userId,$taskInfo['taskId'],0,0,0,0,true);
                }

                $k = array_rand($taskInfo['randNum']);
                $num = $taskInfo['randNum'][$k];

                $newDailyTask->setMaxNum($num);
                $newDailyTask->setStatus(1);

                if ($newDailyTask->isInsert() && !SzUtility::checkArrayKey($taskInfo['taskId'], $newDailyTaskList->getList())) {
                    $newDailyTaskList->addElement($newDailyTask);
                    $newDailyTaskList->setElement($taskInfo['taskId'],$newDailyTask);
                } else {
                    $newDailyTaskList->updateElement($taskInfo['taskId'],$newDailyTask);
                }
            }
        }

        if ($fixedList) {
            foreach ($fixedList as $taskInfo) {
                $newDailyTask = $newDailyTaskList->getElement($taskInfo['taskId'],false);
                if (!$newDailyTask) {
                    $newDailyTask = new NewDailyTaskVo($userId,$taskInfo['taskId'],0,0,0,0,true);
                }

                $key = array_rand($taskInfo['randNum']);
                $num = $taskInfo['randNum'][$key];

                $newDailyTask->setMaxNum($num);
                $newDailyTask->setStatus(1);

                if ($newDailyTask->isInsert() && !SzUtility::checkArrayKey($taskInfo['taskId'], $newDailyTaskList->getList())) {
                    $newDailyTaskList->addElement($newDailyTask);
                    $newDailyTaskList->setElement($taskInfo['taskId'],$newDailyTask);
                } else {
                    $newDailyTaskList->updateElement($taskInfo['taskId'],$newDailyTask);
                }
            }
        }

        $newDailyTaskInfo->setDailyTime(0);
        $newDailyTaskInfo->setUpdateTime($serverTime);
        if ($newDailyTaskInfo->isInsert() && !SzUtility::checkArrayKey(GameConst::DAILY_TASK_INFO_PRIMARY_KEY, $newDailyTaskInfoList->getList())) {
            $newDailyTaskInfoList->addElement($newDailyTaskInfo);
            $newDailyTaskInfoList->setElement(GameConst::DAILY_TASK_INFO_PRIMARY_KEY,$newDailyTaskInfo);
        } else {
            $newDailyTaskInfoList->updateElement(GameConst::DAILY_TASK_INFO_PRIMARY_KEY,$newDailyTaskInfo);
        }

        SzPersister::get()->setVoList($newDailyTaskInfoList);
        SzPersister::get()->setVoList($newDailyTaskList);

        return;
    }

    /**
     * 更新每日任务
     * @param $userId
     * @param $type
     * @param $progress
     * @throws SzException
     */
    public function updateDailyTask($userId,$type,$progress) {
        if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_DAILY_TASK,true)) {
            return;
        }

        $dailyTaskSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TASK_DAILY, null, true);
        if (!$dailyTaskSettings) {
            return;
        }

        $taskIds = array();
        foreach ($dailyTaskSettings as $dailyTaskSetting) {
            if ($dailyTaskSetting['taskType'] == $type) {
                $taskIds[] = $dailyTaskSetting['taskId'];
            }
        }

        if (!$taskIds) {
            return;
        }

        /**
         * @var $newDailyTaskList NewDailyTaskVoList
         * @var $newDailyTask NewDailyTaskVo
         */
        $newDailyTaskList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK);
        foreach ($taskIds as $taskId) {
            $newDailyTask = $newDailyTaskList->getElement($taskId,false);
            if (!$newDailyTask) {
                continue;
            }
            if ($newDailyTask->getNumber() >= $newDailyTask->getMaxNum()) {
                continue;
            }
            if ( ($newDailyTask->getNumber() + $progress) > $newDailyTask->getMaxNum() ) {
                $newDailyTask->setNumber($newDailyTask->getMaxNum());
            } else {
                $newDailyTask->updateNumber($progress);
            }
            $newDailyTaskList->updateElement($taskId,$newDailyTask);
        }

        SzPersister::get()->setVoList($newDailyTaskList);

        return;
    }

    /**
     * 领取每日任务奖励
     * @param $userId
     * @param $taskId
     * @return bool
     * @throws SzException
     */
    public function dailyTaskReward($userId,$taskId) {
        $dailyTaskSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TASK_DAILY, $taskId, true);
        if (!$dailyTaskSetting) {
            throw new SzException(80001,GameConst::CONFIG_GAME_TASK_DAILY.'-'.$taskId);
        }

        /**
         * @var $newDailyTaskList NewDailyTaskVoList
         * @var $newDailyTask NewDailyTaskVo
         */
        $newDailyTaskList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK);
        $newDailyTask = $newDailyTaskList->getElement($taskId);

        if ($newDailyTask->getReceiveStatus()) {
            throw new SzException(90007);
        }

        if (!$newDailyTask->getStatus() || $newDailyTask->getNumber() < $newDailyTask->getMaxNum()) {
            throw new SzException(90051);
        }


        $userLevel = UserService::get()->getUserLevel($userId);
        $dailyLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TASK_LEVEL, $userLevel, true);
        if (!$dailyLevelSetting) {
            throw new SzException(80001,GameConst::CONFIG_GAME_TASK_LEVEL.'-'.$userLevel);
        }
        $outputTid = '';
        foreach ($dailyTaskSetting['reward'] as $entityId=>$count) {
            $count = intval($count * $newDailyTask->getNumber() * $dailyLevelSetting['percentage'] / 100);
            if ($entityId == GameConst::ENTITY_ACTION_GET_EXP) {
                UserService::get()->updateExp($userId, $count,null, false);
            } else {
                GameService::get()->handleEntity($userId, $entityId, $count, true);
            }

            $outputTid .= $entityId.':'.$count.'|';
        }

        $newDailyTask->setReceiveStatus(1);
        $newDailyTaskList->updateElement($taskId,$newDailyTask);
        SzPersister::get()->setVoList($newDailyTaskList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:dailyTask|action:get|',
                'item' => $taskId,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $outputTid,
                'item' => 'dailyTask_get&'.$taskId,
                'value' => 1
            )
        ));

        return true;
    }

    /**
     * 领取宝箱奖励
     * @param $userId
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function dailyTaskBoxReward($userId,$reqTime) {
        $dailyTaskBoxReward = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'dailyTaskBoxReward', true);
        if (!$dailyTaskBoxReward) {
            throw new SzException(80001,ModuleProfileConst::CONFIG_PROFILE.'-dailyTaskBoxReward');
        }
        $dailyTaskBoxReward = json_decode($dailyTaskBoxReward['value'],true);

        /**
         * @var $newDailyTaskInfoList NewDailyTaskInfoVoList
         * @var $newDailyTaskInfo NewDailyTaskInfoVo
         */
        $newDailyTaskInfoList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK_INFO);
        $newDailyTaskInfo = $newDailyTaskInfoList->getElement(GameConst::DAILY_TASK_INFO_PRIMARY_KEY);

        if (date('Y-m-d',$newDailyTaskInfo->getDailyTime()) == date('Y-m-d',$reqTime)) {
            throw new SzException(90007);
        }

        /**
         * @var $newDailyTaskList NewDailyTaskVoList
         */
        $newDailyTaskList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK);
        foreach ($newDailyTaskList->toArray() as $val) {
            if ($val['maxNum'] > $val['number']) {
                throw new SzException(90051);
            }
        }

        $outputTid = '';
        foreach ($dailyTaskBoxReward as $entityId=>$count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $outputTid .= $entityId.':'.$count.'|';
        }

        $newDailyTaskInfo->setDailyTime($reqTime);
        $newDailyTaskInfoList->updateElement(GameConst::DAILY_TASK_INFO_PRIMARY_KEY,$newDailyTaskInfo);
        SzPersister::get()->setVoList($newDailyTaskInfoList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:dailyTask|action:award|',
                'item' => 'dailyTask_award',
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $outputTid,
                'item' => 'dailyTask_award',
                'value' => 1
            )
        ));

        return true;
    }

    /**
     * 每日任务信息
     * @param $userId
     * @return array
     */
    public function dailyTaskInfo($userId) {
        /**
         * @var $newDailyTaskInfoList NewDailyTaskInfoVoList
         * @var $newDailyTaskList NewDailyTaskVoList
         */
        $newDailyTaskInfoList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK_INFO);
        $newDailyTaskList = SzPersister::get()->getVoList($userId,GameConst::ORM_NAME_DAILY_TASK);
        return array(
            'newDailyTaskInfoList' => $newDailyTaskInfoList->toArray(),
            'newDailyTaskList' => $newDailyTaskList->toArray(),
        );
    }
}
